package com.atguigu.ucenterservice.service.impl;

import com.atguigu.commonutils.jwt.JwtUtils;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.ucenterservice.entity.UcenterMember;
import com.atguigu.ucenterservice.entity.vo.RegistVo;
import com.atguigu.ucenterservice.mapper.UcenterMemberMapper;
import com.atguigu.ucenterservice.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 老王
 * @since 2020-05-23
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //根据手机号密码登陆
    @Override
    public String login(UcenterMember ucenterMember) {
        //1.判断手机号密码是否为空
        if (StringUtils.isEmpty(ucenterMember.getMobile()) || StringUtils.isEmpty(ucenterMember.getPassword())) {
            throw new GuliException(20001,"登陆失败");
        }
        //2.不为空则用手机号查出用户
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", ucenterMember.getMobile());
        UcenterMember member = baseMapper.selectOne(wrapper);

        if (member==null || StringUtils.isEmpty(member.getPassword())){
            throw new GuliException(20001,"登陆失败");
        }
        //3.查出来看密码不为空且跟前端传来密码进行比较
        //转MD5
        String password= DigestUtils.md5DigestAsHex(ucenterMember.getPassword().getBytes(StandardCharsets.UTF_8));
        if (!password.equals(member.getPassword())){
            throw new GuliException(20001,"密码错误");
        }

        //4.看这个用户是否是禁用状态
        if (member.getIsDisabled()){
            throw new GuliException(20001,"对不起，你已被拉进黑名单");
        }

        //5.用jwt生成token返回
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }

    //手机号密码注册
    @Override
    public String regist(RegistVo registVo) {
        String code = registVo.getCode();
        String mobile = registVo.getMobile();
        String nickname = registVo.getNickname();
        String password = registVo.getPassword();

        //判空
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001, "注册失败");
        }

        //获取redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (StringUtils.isEmpty(redisCode) || !code.equals(redisCode)){
            throw new GuliException(20001, "验证码错误");
        }

        //数据库判断是否已经注册过
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new GuliException(20001, "该手机号已被注册");
        }

        //把数据添加进数据库
        UcenterMember member = new UcenterMember();
        member.setNickname(nickname);
        member.setPassword(DigestUtils.md5DigestAsHex(password.getBytes(StandardCharsets.UTF_8)));
        member.setMobile(mobile);
        baseMapper.insert(member);

        //返回token
        return JwtUtils.getJwtToken(member.getId(), nickname);
    }

    //根据openid获取用户
    @Override
    public UcenterMember getByOpenId(String openId) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openId);
        return baseMapper.selectOne(wrapper);
    }
}
